﻿using System.Collections.Generic;
using System.Linq;

namespace EastForceCondensor.CommCtrl
{
    public enum SensorType { TC, PT100, VCoil, Cernox, Battery };
    public struct CalibData
    {
        public float ad;
        public float temp;
        public CalibData(float _ad, float _temp)
        {
            ad = _ad;
            temp = _temp;
        }
    }
    public struct CalibDataGroup
    {
        public List<CalibData> batterPcnt;
        public CalibDataGroup(int tcNum, int ptNum, int cernoxNum)
        {
            batterPcnt = new List<CalibData>();
        }
    }
    public class DataConverter
    {
        private CalibDataGroup m_calibData = new CalibDataGroup(1, 4, 10);

        private readonly float resisOhm = 100f;
        private float m_currentIntensity_pt100 = 1f;
        private float m_currentIntensity_cernox = 1f;
        public DataConverter()
        {
            GetCalibData();
        }
        public void GetCalibData()
        {
            m_calibData.batterPcnt.Add(new CalibData(2.5f, 0));
            m_calibData.batterPcnt.Add(new CalibData(3.5f, 0.05f));
            m_calibData.batterPcnt.Add(new CalibData(3.68f, 0.10f));
            m_calibData.batterPcnt.Add(new CalibData(3.7f, 0.15f));
            m_calibData.batterPcnt.Add(new CalibData(3.73f, 0.20f));
            m_calibData.batterPcnt.Add(new CalibData(3.77f, 0.30f));
            m_calibData.batterPcnt.Add(new CalibData(3.79f, 0.40f));
            m_calibData.batterPcnt.Add(new CalibData(3.82f, 0.50f));
            m_calibData.batterPcnt.Add(new CalibData(3.87f, 0.60f));
            m_calibData.batterPcnt.Add(new CalibData(3.93f, 0.70f));
            m_calibData.batterPcnt.Add(new CalibData(4.0f, 0.80f));
            m_calibData.batterPcnt.Add(new CalibData(4.08f, 0.90f));
            m_calibData.batterPcnt.Add(new CalibData(4.2f, 1.0f));

        }
        public float ConvertToPercnt(float ad)
        {
            float pcnt = ConvertToPercnt(ad, ref m_calibData.batterPcnt);
            return pcnt;
        }
        private float ConvertToPercnt(float ad, ref List<CalibData> calibList)
        {
            if (calibList.Count() == 0)
                return 0.0f;
            if (ad < calibList.First().ad)
                return 0;
            if (ad > calibList.Last().ad)
                return 1;

            float re = 0f;
            for (int i = 0; i < calibList.Count() - 1; i++)
            {
                if (ad >= calibList.ElementAt(i).ad && ad <= calibList.ElementAt(i + 1).ad)
                {
                    re = (ad - calibList.ElementAt(i).ad) * (calibList.ElementAt(i + 1).temp - calibList.ElementAt(i).temp) / (calibList.ElementAt(i + 1).ad - calibList.ElementAt(i).ad) + calibList.ElementAt(i).temp;
                    break;
                }
            }
            return re;
        }
        public void GetCurrentIntensity_pt100(float ad)
        {
            m_currentIntensity_pt100 = ad / resisOhm;
        }
        public void GetCurrentIntensity_cernox(float ad)
        {
            m_currentIntensity_cernox = ad / resisOhm;
        }
    }
}
